﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Globalization;
using System.Threading;
using System.Collections;
using System.Runtime.CompilerServices;
using System.Security.Principal;

namespace Enterprise.Core
{
    public class CurrentContext
    {
        public static void Initial(int employeeId, string loginName)
        {
            ClearContextStorage();
            PutIntoStorage("CurrentContext::EmployeeID::"+CurrentUser.Identity.Name, employeeId);
        }

        public static IPrincipal CurrentUser
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    return HttpContext.Current.User;
                }
                return null;
            }
            set
            {
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.User = value;
                }
            }
        } 

        public static CultureInfo Culture()
        {
            return Thread.CurrentThread.CurrentCulture;
        }

         public static string GetConfigSetting(string Key)
         {
             return string.Empty; 
         }

         public static int GetEmployeeID(string NetworkLogin)
         {
             if (GetFromStorage("CurrentContext::EmployeeID::" + NetworkLogin) == null)
             { 
                //Accesd db
                 PutIntoStorage("CurrentContext::EmployeeID::" + NetworkLogin, 0);
             }
             return (int)GetFromStorage("CurrentContext::EmployeeID::" + NetworkLogin);
         }

         public static string LoginName()
         {
             return CurrentUser.Identity.Name;
         }

         #region WebStore
        
         internal static Hashtable ContextStorageObject
         {
             get
             {
                if (HttpContext.Current.Session[msWebStorage] == null)
                {
                    HttpContext.Current.Session[msWebStorage] = new Hashtable();
                }
                return (Hashtable)HttpContext.Current.Session[msWebStorage];
             }
         }

         private static string msWebStorage="WebStororage";
         public static object GetFromStorage(string Key)
         {
             return ContextStorageObject[Key];
         }

         public static void PutIntoStorage(string Key, object Value)
         {
             ContextStorageObject[Key] = RuntimeHelpers.GetObjectValue(Value);
         }

         public static void RemoveFromStorage(string Key)
         {
             ContextStorageObject.Remove(Key);
         }

         public static void ClearContextStorage()
         {
             HttpContext.Current.Session[msWebStorage] = null;
         }
         #endregion

    }
}
